﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using Payroll.Common.Utils;
using PayrollHR.Web;
using Payroll.BusinessLogic.Package;

namespace PayrollHR.Administration.Modules
{
    public partial class PackageAdd : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pnlYesNo.Visible = false;
                BinCustomer();
            }
        }
        protected void BinCustomer()
        {
         //   ctrlCustomerInfo.FillDropDown();
            if (this.PackageId == 0)
                return;
            var pack = PackageService.GetPackageById(this.PackageId);
            if (CommonHelper.IsNull(pack))
                return;
            ctrlPackeInfo.BindPackage(pack);
          //  ctrlCustomerInfo.BindCustomer(customer);
        }
        public int PackageId
        {
            get
            {
                return CommonHelper.QueryStringInt("PackageId");
            }
        }
        protected void Continue_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    SaveButton.Enabled = false;
                    SaveAndStayButton.Enabled = false;
                    this.Save();
                    Response.Redirect("ListPackage.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }

        protected void btnSaveAndStay_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    SaveButton.Enabled = false;
                    SaveAndStayButton.Enabled = false;
                    Package pack = Save();
                    Response.Redirect(String.Format("CustomerAdd.aspx?PackageId={0}", pack.PackagesID));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(String.Format("PackageAdd.aspx?PackageId={0}", 0));
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }
        protected Package Save()
        {
            Package pack = ctrlPackeInfo.GetPackage();
            PackageService.Save(pack);

            return pack;
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                lblPackage.Visible = false;
                try
                {
                    Package pack= ctrlPackeInfo.GetPackage();
                    if (pack.PackagesID == 0)
                    {
                        var cust = PackageService.GetPackageByName(pack.Name);
                        if (CommonHelper.IsNull(cust))
                        {
                            SaveButton.Enabled = false;
                            SaveAndStayButton.Enabled = false;
                            this.Save();
                            Response.Redirect("ListPackage.aspx");
                        }
                        else
                        {
                            lblPackage.Visible = true;
                      
                            pnlYesNo.Visible = true;
                        }
                    }
                    else
                    {
                        this.Continue_Click(sender, e);
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }

        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    lblPackage.Visible = false;
                    Package pack = ctrlPackeInfo.GetPackage();
                    if (pack.PackagesID == 0)
                    {
                        var cust = PackageService.GetPackageByName(pack.Name);
                        if (CommonHelper.IsNull(cust))
                        {
                            SaveButton.Enabled = false;
                            SaveAndStayButton.Enabled = false;
                            this.Save();
                            Response.Redirect(String.Format("PackageAdd.aspx?PackageId={0}", pack.PackagesID));
                        }
                        else
                        {
                            lblPackage.Visible = true;
                          //  lbMessage.Text = String.Format("Khách hàng tên {0} đã có, bạn tiếp tục lưu nhấn button kế bên.", pack.Name);
                            pnlYesNo.Visible = true;
                        }
                    }
                    else
                    {
                        this.Continue_Click(sender, e);
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
    }
}